/*
 * 
 *  ${model.comment}Controller 创建于 2016-10-12 11:54:23 版权归作者和作者当前组织所有
 */
package ${model.packageName}.${model.modulePackage}.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.web.servlet.ModelAndView;

import com.mmk.common.model.EasyPageable;
import com.mmk.common.model.GridData;
import com.mmk.common.model.ResultMsg;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import ${model.packageName}.${model.modulePackage}.service.${model.model}Service;
import ${model.packageName}.${model.modulePackage}.model.${model.model};
import ${model.packageName}.${model.modulePackage}.condition.${model.model}Condition;

/**
*@Title: ${model.model}Controller
*@Description: ${model.comment} 的web控制层
*@author ${model.author}
*/
@RestController
public class ${model.model}Controller {
    
    private Log log = LogFactory.getLog(this.getClass());
    
    @Resource 
    private ${model.model}Service ${model.modelL}Service;

    /**
     * 跳转至列表页面
     * @return 返回页面以及页面模型
     */
    @RequestMapping("/${model.modulePackage}/${model.modelL}/list")
    public ModelAndView list(){
        log.info("${model.comment}列表查询");
        ModelAndView modelAndView = new ModelAndView("${model.modulePackage}/${model.modelL}/list");
        return  modelAndView;
    }
    
    
    /**
     * 加载表格数据 用户
     * 
     * @param ${model.modelL}Condition
     *            用户查询参数
     * @param pageable
     *            分页参数
     * @return 查询所得数据
     */
    @RequestMapping("/${model.modulePackage}/${model.modelL}/gridData")
    @ResponseBody
    public GridData<${model.model}> loadList(${model.model}Condition ${model.modelL}Condition, EasyPageable pageable){
        log.info("获取${model.comment}列表数据");
        Page<${model.model}> ${model.modelL}Page = ${model.modelL}Service.list(${model.modelL}Condition,pageable.pageable());   
        GridData<${model.model}> grid = new GridData<${model.model}>(${model.modelL}Page);
        return grid;
    }
    
    /**
     * 新增页面
     * @return 跳转到${model.comment}新增页面
     */
    @RequestMapping("/${model.modulePackage}/${model.modelL}/add")
    public ModelAndView addPage(){
        ModelAndView modelAndView = new ModelAndView("${model.modulePackage}/${model.modelL}/form");
        modelAndView.addObject("${model.modelL}", new ${model.model}());
        return modelAndView;
    }
    
    /**
     * 跳转到编辑页面
     * @param ${model.modelL}  跳转到编辑页面
     */ 
    @RequestMapping("/${model.modulePackage}/${model.modelL}/edit")
    public ModelAndView editPage(${model.model} ${model.modelL}){
        log.info("${model.comment}编辑页面");
        ${model.modelL} = ${model.modelL}Service.find(${model.modelL}.get${pkFieldUp}());
        ModelAndView modelAndView = new ModelAndView("${model.modulePackage}/${model.modelL}/form");
        modelAndView.addObject("${model.modelL}", ${model.modelL});
        return modelAndView ;
    }
    
    
    /**
     * ${model.comment}数据保存方法
     * @param ${model.modelL} 要保存的数据
     * @return ${model.modelL} 保存后的数据
     */
    @RequestMapping("/${model.modulePackage}/${model.modelL}/save")
    @ResponseBody
    public ResultMsg save(${model.model} ${model.modelL}){
        log.info("${model.comment}保存");
        try {
            ${model.modelL}Service.save(${model.modelL});
        } catch (Exception e) {
        	log.error(e.getMessage(),e);
            return new ResultMsg(false,"${model.comment}保存失败");
        }
        return new ResultMsg(true,"${model.comment}保存成功");
    }
    
   
    
    /**
     * 跳转至详细信息页面
     * @param ${model.modelL} 参数
     * @return 详情数据
     */ 
    @RequestMapping("/${model.modulePackage}/${model.modelL}/details")
    @ResponseBody
    public ${model.model} details(${model.model} ${model.modelL}){
        log.info("${model.comment}详细信息");
        ${model.modelL} = ${model.modelL}Service.find(${model.modelL}.get${pkFieldUp}());
        return ${model.modelL};
    }
    
    /**
     * 删除数据操作组方法
     * @param page ${model.modelL}
     * @return
     */
    @RequestMapping("/${model.modulePackage}/${model.modelL}/delete")
    public ResultMsg delete(${model.model} ${model.modelL}){
        log.info("${model.comment}删除");
        try {
            ${model.modelL}Service.delete(${model.modelL});
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return new ResultMsg(false, "删除失败");
        }
        return new ResultMsg(true,"删除成功"); 
    }
    
    /**
     * 批量删除数据操作组方法
     * @param page ${model.modelL}
     * @return ture or false 如果成功返回true ,出现错误返回false
     */
    @RequestMapping("/${model.modulePackage}/${model.modelL}/deleteAll")
    public boolean delete(List<${model.model}> ${model.modelL}List){
        log.info("${model.comment}批量删除");
        try {
            ${model.modelL}Service.delete(${model.modelL}List);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return false;
        }
        return true; 
    }
    
#foreach($field in ${fieldList})
#if($field.existsMethod)
    /**
     * 跳转至详细信息页面
     * @param  ${field.field} 检查字段是否存在
     * @return  true or false
     */ 
    @RequestMapping("/${model.modulePackage}/${model.modelL}/${field.field}/exist")
    @ResponseBody
    public boolean exists${field.fieldUp}(${field.type} ${field.field}){
        log.info("检测${model.comment}是否存在  ${field.field}");
        return ${model.modelL}Service.exists${field.fieldUp}(${field.field});
    }
#end
#end
}